let cacheName = 'pwa-test'

let fileToCache = [
  // './',
  './static/css/style.css',
  './static/images/Git_144px.png',
  './static/js/app.js',
  'https://cdn.jsdelivr.net/npm/vue',
  'https://unpkg.com/mint-ui/lib/style.css',
  'https://unpkg.com/mint-ui/lib/index.js'
]

self.addEventListener('install', event => {
  console.log('Service Worker: Installing...')

  event.waitUntil(
    caches.open(cacheName).then(cache => {
      console.log('Service Worker: Caching App Shell at the moment...', cache)
      console.log('fileToCache', fileToCache)
      return cache.addAll(fileToCache)
    })
  )
})

self.addEventListener('activate', event => {
  console.log('Service Worker: Activating...')

  event.waitUntil(
    caches.keys().then(cacheNames => {
      return Promise.all(cacheNames.map(key => {
        console.warn('cacheNamesKey', key)
        if (key !== cacheName) {
          console.log('Service Worker: Removing Old Cache', key)
          return caches.delete(key)
        }
      }))
    })
  )

  return self.clients.claim()
})

self.addEventListener('fetch', event => {
  console.log('Service Worker: Fetch', event.request.url)

  event.respondWith(
    caches.match(event.request).then(res => {
      return res || fetch(event.request)
    })
  )
})